package com.jhclothing.web.controller.outbound;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.jhclothing.common.utils.StringUtils;
import com.jhclothing.product.domain.Fabric;
import com.jhclothing.product.service.IFabricService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jhclothing.common.annotation.Log;
import com.jhclothing.common.core.controller.BaseController;
import com.jhclothing.common.core.domain.AjaxResult;
import com.jhclothing.common.enums.BusinessType;
import com.jhclothing.outbound.domain.FabricOutbound;
import com.jhclothing.outbound.service.IFabricOutboundService;
import com.jhclothing.common.utils.poi.ExcelUtil;
import com.jhclothing.common.core.page.TableDataInfo;

/**
 * 布料出库Controller
 *
 * @author zyp
 * @date 2024-04-25
 */
@RestController
@RequestMapping("/outbound/fabric")
public class FabricOutboundController extends BaseController {
    @Autowired
    private IFabricOutboundService fabricOutboundService;
    @Autowired
    private IFabricService fabricService;

    /**
     * 查询布料出库列表
     */
    @PreAuthorize("@ss.hasPermi('outbound:fabric:list')")
    @GetMapping("/list")
    public TableDataInfo list(FabricOutbound fabricOutbound) {
        startPage();
        List<FabricOutbound> list = fabricOutboundService.selectFabricOutboundList(fabricOutbound);
        return getDataTable(list);
    }

    /**
     * 导出布料出库列表
     */
    @PreAuthorize("@ss.hasPermi('outbound:fabric:export')")
    @Log(title = "布料出库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FabricOutbound fabricOutbound) {
        List<FabricOutbound> list = fabricOutboundService.selectFabricOutboundList(fabricOutbound);
        ExcelUtil<FabricOutbound> util = new ExcelUtil<FabricOutbound>(FabricOutbound.class);
        util.exportExcel(response, list, "布料出库数据");
    }

    /**
     * 获取布料出库详细信息
     */
    @PreAuthorize("@ss.hasPermi('outbound:fabric:query')")
    @GetMapping(value = {"/", "/{fabricOutboundId}"})
    public AjaxResult getInfo(@PathVariable(value = "fabricOutboundId", required = false) Long fabricOutboundId) {
        AjaxResult ajax = AjaxResult.success();
        List<Fabric> fabrics = fabricService.selectFabricAll();
        ajax.put("fabrics", fabrics);
        if (StringUtils.isNotNull(fabricOutboundId)) {
            FabricOutbound fabricOutbound = fabricOutboundService.selectFabricOutboundByOutboundId(fabricOutboundId);
            ajax.put(AjaxResult.DATA_TAG, fabricOutbound);
            ajax.put("fabricId", fabricOutbound.getFabricId());
        }
        return ajax;
    }

    /**
     * 新增布料出库
     */
    @PreAuthorize("@ss.hasPermi('outbound:fabric:add')")
    @Log(title = "布料出库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FabricOutbound fabricOutbound) {
        Fabric fabric = fabricService.selectFabricByFabricId(fabricOutbound.getFabricId());
        String fabricCodeAndName = fabric.getFabricCodeAndName();
        if (fabric.getNowInventory().compareTo(fabricOutbound.getOutboundNum()) < 0) {
            return error(String.format("布料 %s 库存不足，请重新选择出库数量", fabricCodeAndName));
        }
        BigDecimal fabricInventory = fabric.getNowInventory().subtract(fabricOutbound.getOutboundNum());
        fabricOutbound.setCreateBy(getUsername());
        return toAjax(fabricOutboundService.insertFabricOutbound(fabricOutbound, fabricInventory));
    }

    /**
     * 修改布料出库
     */
    @PreAuthorize("@ss.hasPermi('outbound:fabric:edit')")
    @Log(title = "布料出库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FabricOutbound fabricOutbound) {
        fabricOutbound.setUpdateBy(getUsername());
        String result = fabricOutboundService.updateFabricOutbound(fabricOutbound);
        if (!"SUCCESS".equals(result)) {
            return error(result);
        }

        return AjaxResult.success();
    }

    /**
     * 删除布料出库
     */
    @PreAuthorize("@ss.hasPermi('outbound:fabric:remove')")
    @Log(title = "布料出库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{fabricOutboundIds}")
    public AjaxResult remove(@PathVariable Long[] fabricOutboundIds) {
        return toAjax(fabricOutboundService.deleteFabricOutboundByOutboundIds(fabricOutboundIds));
    }
}
